package com.group.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.group.dao.TbDeviceAttrMapper;
import com.group.dao.TbDeviceMapper;
import com.group.pojo.Common;
import com.group.pojo.TbAttrName;
import com.group.pojo.TbDevice;
import com.group.pojo.TbDeviceAttr;
import com.group.pojo.TbDeviceExample;
import com.group.pojo.TbDeviceExample.Criteria;
import com.group.service.AttrNameService;
import com.group.service.CategoryService;
import com.group.service.CommonService;
import com.group.service.DeviceService;
import com.group.util.CommonUtil;

@Service
public class DeviceServiceImpl implements DeviceService {
	@Autowired
	private TbDeviceMapper deviceMapper;
	@Autowired
	private CategoryService categoryService;

	// 通过设备名称查询设备id
	@Override
	public String getDeviceIdByDeviceServiceNumber(String deviceServiceNumber) {
		List<TbDevice> lsit = deviceMapper.selectByExample(null);
		for (TbDevice device : lsit) {
			if (deviceServiceNumber.equals(device.getDeviceServiceNumber())) {
				return device.getId();
			}
		}
		return null;
	}


	// 根据设备名称片段查询设备信息
	@Override
	public List<TbDevice> getDeviceInfoByNameFragment(String nameFragment) {

		return null;
	}

	// 根据设备名称查询设备是否存在
	@Override
	public String checkDeviceName(String deviceName) {
		TbDeviceExample example = new TbDeviceExample();
		TbDeviceExample example1 = new TbDeviceExample();
		example.createCriteria().andDeviceNameEqualTo(deviceName).andDeviceStatusEqualTo("1");
		example1.createCriteria().andDeviceNameEqualTo(deviceName).andDeviceStatusEqualTo("2");
		int cunt = deviceMapper.countByExample(example);
		int cunt1 = deviceMapper.countByExample(example1);
		if (cunt == 0) {
			return "false";
		}
		return "true";
	}

	 
	// 通过设备id查询设备
	@Override
	public List<TbDevice> getDeviceById(String id) {
		TbDeviceExample example = new TbDeviceExample();
		example.createCriteria().andIdEqualTo(id);
		List<TbDevice> list = deviceMapper.selectByExample(example);
		return list;
	}

	// 修改设备信息
	@Override
	public Map<String, String> updateDevice(TbDevice device, MultipartFile pictureFile1, HttpServletRequest request) {
		TbDevice newdevice = new TbDevice();
		newdevice.setId(device.getId());
		newdevice.setDeviceName(device.getDeviceName());
		newdevice.setDeviceModel(device.getDeviceModel());
		newdevice.setDeviceServiceNumber(device.getDeviceServiceNumber());
		newdevice.setCategoryId(categoryService.getCategoryIdByName(device.getCategoryId()));
		newdevice.setDeviceDesc(device.getDeviceDesc());
		// 图片操作
		if (pictureFile1.getSize() != 0) {// 判断是否有文件上传
			String name = UUID.randomUUID().toString().replaceAll("-", "");// 重新起以个名称
			String ext = FilenameUtils.getExtension(pictureFile1.getOriginalFilename());// 截获图片的格式
			String path = request.getSession().getServletContext().getRealPath("myImg/"); // 获得myImg的项目路径
			name = name + "." + ext;
			try {
				pictureFile1.transferTo(new File(path + "\\" + name));// 保存到项目myImg文件夹下面
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			newdevice.setDeviceImageUrl(name);// 储存图片的名称
		}
		// 提交到数据库
		int insert = deviceMapper.updateByPrimaryKeySelective(newdevice);
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}

	// 根据设备id删除设备id
	@Override
	public Map<String, String> deleteDeviceInfo(String[] deviceIds) {
		TbDevice device = new TbDevice();
		device.setDeviceStatus("0");
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		for (String customerId : deviceIds) {
			device.setId(customerId);
			int update = deviceMapper.updateByPrimaryKeySelective(device);
			if (update != 1) {
				map.put("success", "false");
				return map;
			}
		}
		return map;
	}

	// 检查设备服务号是否存在
	@Override
	public String CheckdeviceServiceNumber(String deviceServiceNumber, String deviceName) {
		TbDeviceExample example = new TbDeviceExample();
		example.createCriteria().andDeviceNameEqualTo(deviceName).andDeviceServiceNumberEqualTo(deviceServiceNumber)
				.andDeviceStatusEqualTo("1");
		TbDeviceExample example1 = new TbDeviceExample();
		example.createCriteria().andDeviceNameEqualTo(deviceName).andDeviceServiceNumberEqualTo(deviceServiceNumber)
				.andDeviceStatusEqualTo("2");
		int count = deviceMapper.countByExample(example);
		int count1 = deviceMapper.countByExample(example1);
		if (count != 1&& count1!=1) {
			return "true";
		}
		return "false";
	}

	// 根据设备id查询设备名称
	@Override
	public String getdeviceNameById(String deviceId) {
		TbDeviceExample example = new TbDeviceExample();
		example.createCriteria().andIdEqualTo(deviceId);
		String deviceName = deviceMapper.selectByExample(example).get(0).getDeviceName();
		return deviceName;
	}

	// 根据设备名称查询设备id
	public String getDeviceIdByName(String name) {
		TbDeviceExample example = new TbDeviceExample();
		example.createCriteria().andDeviceNameEqualTo(name);
		String deviceId = deviceMapper.selectByExample(example).get(0).getId();
		return deviceId;
	}

	// 修改设备时检查设备以及对应的服务号是否存在
	@Override
	public String updateCheckdeviceServiceNumber(String deviceServiceNumber, String deviceName, String id) {
		TbDeviceExample example = new TbDeviceExample();
		TbDeviceExample example1 = new TbDeviceExample();
		TbDeviceExample example2 = new TbDeviceExample();
		example.createCriteria().andDeviceNameEqualTo(deviceName).andDeviceServiceNumberEqualTo(deviceServiceNumber)
				.andDeviceStatusEqualTo("1");
		example2.createCriteria().andDeviceNameEqualTo(deviceName).andDeviceServiceNumberEqualTo(deviceServiceNumber)
				.andDeviceStatusEqualTo("2");
		example1.createCriteria().andIdEqualTo(id).andDeviceStatusEqualTo("1");
		List<TbDevice> list = deviceMapper.selectByExample(example);
		List<TbDevice> list2 = deviceMapper.selectByExample(example2);
		if(list.size()==0){
			list = list2;
		}
		List<TbDevice> list1 = deviceMapper.selectByExample(example1);
		if (list.get(0).getDeviceServiceNumber().equals(list1.get(0).getDeviceServiceNumber())) {
			return "true";
		}
		int count = deviceMapper.countByExample(example);
		if (count != 1) {
			return "true";
		} else {
			return "false";
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	// 获得所有的设备信息
	@Override
	public List<TbDevice> query(TbDevice device) {
		TbDeviceExample example = new TbDeviceExample();
		setCondition(example.createCriteria(), device);
		
		List<TbDevice> list = deviceMapper.selectByExample(example);
		return list;
	}
	private void setCondition(Criteria criteria, TbDevice device) {
		String deviceStatus = device.getDeviceStatus();
		if (deviceStatus != null && !"".equals(deviceStatus)) {
			criteria.andDeviceStatusEqualTo(deviceStatus);
		}
		
		String id = device.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String categoryId = device.getCategoryId();
		if (categoryId != null && !"".equals(categoryId)) {
			criteria.andCategoryIdEqualTo(categoryId);
		}
		
		String deviceServiceNumber = device.getDeviceServiceNumber();
		if (deviceServiceNumber != null && !"".equals(deviceServiceNumber)) {
			criteria.andDeviceServiceNumberEqualTo(deviceServiceNumber);
		}
	}


 
 
	 
 

}
